package com.riggie.extension.listener;

import com.rabbitmq.client.Channel;
import com.riggie.extension.constant.Constants;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Date;


@Component
public class DLListener {

    //@RabbitListener(queues = Constants.NORMAL_QUEUE)
    public void handMessage(Message message, Channel channel) throws IOException, InterruptedException {
        long deliberytag = message.getMessageProperties().getDeliveryTag();

       try{
           //消费者逻辑
           System.out.printf("[normal.queue]接收到消息：%s， deliberytag: %d \n",new String(message.getBody(),"UTF-8"),message.getMessageProperties().getDeliveryTag());

           //进行业务逻辑处理
           System.out.println("业务逻辑处理");
           //模拟处理失败
           int num = 3 / 0;
           System.out.println("业务处理完成");
           //肯定确认
           channel.basicAck(deliberytag,false);
       } catch (Exception e){
           Thread.sleep(1000);
           //否定确认，拒绝接受
           //第二个false，代表不进入队列。那么此时就会进入死信队列
           channel.basicNack(deliberytag,false,false);
       }
    }

    @RabbitListener(queues = Constants.DL_QUEUE)
    public void dlHandMessage(Message message, Channel channel) throws IOException {
        //消费者逻辑
        System.out.printf("[dl.queue] %tc 接收到消息： %s， deliberytag: %d \n", new Date(),new String(message.getBody(),"UTF-8"),message.getMessageProperties().getDeliveryTag());

    }
}
